/*
 *  (C) Copyright 2008 Insula T.I. Ltda.
 * 
 *  This file is part of Vulcano.
 *
 *  Vulcano is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU Lesser General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  Vulcano is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public License
 *  along with Vulcano.  If not, see <http://www.gnu.org/licenses/>.
 */
package org.openinsula.vulcano.orm.dao.query.builder;

import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import static org.openinsula.vulcano.orm.dao.query.spec.SpecFactory.eq;
import static org.openinsula.vulcano.orm.dao.query.spec.SpecFactory.gt;
import static org.openinsula.vulcano.orm.dao.query.spec.SpecFactory.or;

import org.junit.Test;
import org.openinsula.vulcano.orm.dao.query.DaoQuery;
import org.openinsula.vulcano.orm.dao.query.DistinctDaoQuery;
import org.openinsula.vulcano.orm.dao.query.ProjectionDaoQuery;
import org.openinsula.vulcano.orm.dao.query.SimpleDaoQuery;
import org.openinsula.vulcano.orm.dao.query.spec.AndSpec;
import org.openinsula.vulcano.orm.entity.Person;
import org.openinsula.vulcano.orm.entity.Pet;

public class DaoSelectTestCase {

	@Test
	public void testSyntax() {
		DaoQuery<Person> personDaoQuery = new DaoSelect<Person>().all().from(Person.class).eq("name", "Obi Wan").order("name").first(0).limit(10).lock().toQuery();
		
		assertTrue(personDaoQuery instanceof SimpleDaoQuery);
		assertEquals(Person.class, personDaoQuery.getQueryClass());
		assertTrue(personDaoQuery.getSpec() instanceof AndSpec);
		assertEquals(1, ((AndSpec)personDaoQuery.getSpec()).getExpressions().size());
		assertArrayEquals(new String[] {"name"}, personDaoQuery.getOrderParameters().toArray());
		assertEquals(0, personDaoQuery.getFirstResult());
		assertEquals(10, personDaoQuery.getLimit());
		assertTrue(personDaoQuery.isLock());
		
		personDaoQuery = new DaoSelect<Person>(true).all().from(Person.class).eq("name", "Obi Wan").order("name", "age").limit(20).toQuery();
		
		assertTrue(personDaoQuery instanceof DistinctDaoQuery);
		assertEquals(Person.class, personDaoQuery.getQueryClass());
		assertTrue(personDaoQuery.getSpec() instanceof AndSpec);
		assertEquals(1, ((AndSpec)personDaoQuery.getSpec()).getExpressions().size());
		assertArrayEquals(new String[] {"name", "age"}, personDaoQuery.getOrderParameters().toArray());
		assertEquals(20, personDaoQuery.getLimit());
		assertFalse(personDaoQuery.isLock());
		
		personDaoQuery = new DaoSelect<Person>(true).all().from(Person.class).spec(or(gt("age", 10), eq("jedi", false))).eq("name", "Obi Wan").limit(10).toQuery();
		assertEquals(Person.class, personDaoQuery.getQueryClass());
		assertTrue(personDaoQuery.getSpec() instanceof AndSpec);
		assertEquals(2, ((AndSpec)personDaoQuery.getSpec()).getExpressions().size());
		assertEquals(0, personDaoQuery.getOrderParameters().size());
		assertEquals(10, personDaoQuery.getLimit());
		assertFalse(personDaoQuery.isLock());
		
		assertTrue(personDaoQuery instanceof DistinctDaoQuery);
		
		DaoQuery<Integer> projectionDaoQuery = new DaoSelect<Integer>().count().from(Pet.class).first(5).toQuery();
		
		assertTrue(projectionDaoQuery instanceof ProjectionDaoQuery);
		assertEquals(Pet.class, projectionDaoQuery.getQueryClass());
		assertNull(projectionDaoQuery.getSpec());
		assertEquals(0, projectionDaoQuery.getOrderParameters().size());
		assertEquals(5, projectionDaoQuery.getFirstResult());
		assertFalse(projectionDaoQuery.isLock());
	}
	
}
